package bultin;

import javax.swing.ButtonGroup;
import javax.swing.JComponent;

import pseudo_ASH.ErrorSeriousCondition;
import pseudo_ASH.MainButtonPanel;
import code.ClassC;
import code.ErrorCondition;
import code.TokenType;
import code.Variable;

import com.sydwell.service.ArrayPlus;

public abstract class BIClass extends ClassC implements MakeJavaObjInterface {
	protected Variable returnVariable = new Variable("returnVariable", null);
	//private static ArrayPlus<BIClass> inClasses = new ArrayPlus<BIClass>();
	private static ArrayPlus<JComponent> javaObj = new ArrayPlus<JComponent>();
	private static ArrayPlus<Variable> StaticObjects = new ArrayPlus<Variable>();


	protected static ButtonGroup currentBG = new ButtonGroup();
	//private static String theButtonGroupName;

	public BIClass(String className) {
		super(className);
		super.builtClass = true;
	}

	/**
	 * Call this method to ensure that builtinClasses have been registered
	 */
	public static void initializeClasses(){
		//new JavaObjectMethods();
		JavaObjectMethods.loadAll();
		ClassC.addClass(new System_Bi());
		ClassC.addClass(new String_Bi());
		ClassC.addClass(new Button_Bi());
		ClassC.addClass(new RadioButton_Bi());
		ClassC.addClass(new CheckBox_Bi());
		ClassC.addClass(new TextField_Bi());
		ClassC.addClass(new Label_Bi());
		ClassC.addClass(new Picture_Bi());
	}

	public static ArrayPlus<Variable> getStaticObjects() {
		return StaticObjects;
	}

	public static Variable getStaticObjectOnName(String name) {
		for (Variable mcc: StaticObjects) {
			if (mcc.getName().equals(name)) {
				return mcc;
			}
		}
		return null;
	}
	/**
	 * Should be enabled after code executes normally via the END keyword
	 * Must be disabled during Action function calls {@link MainButtonPanel.FunctionButtonListener}
	 * 
	 * @param value 
	 */
	public static void enableJObjects(boolean value) {
		for (JComponent jos: javaObj) {
			jos.setEnabled(value);
		}
	}

	//	/**
	//	 * @param classname
	//	 * @param objectName
	//	 * @param arrayPlus 
	//	 * @return the Variable
	//	 * @throws ErrorSeriousCondition 
	//	 */
	//	public static JComponent makeFrom(String classname,
	//			String objectName, ArrayPlus<TokenType> arrayPlus)
	//	throws ErrorSeriousCondition {
	//		Variable ret = Variable.aNULL("unnSETTT");
	//
	//		JComponent jo = null;
	//		//ObjectC object = null;
	//		if (arrayPlus == null || arrayPlus.size() < 1) {
	//			throw new ErrorSeriousCondition("Constructors of Built-in objects require String arguments ");
	//		}
	//		if (classname.equals("Button")) {
	//			jo = new JButton(arrayPlus.get(0).getTokenText());
	//			ret.setName(objectName);
	//			//object = ObjectC.createBuiltinObjects("Button", false);
	//			//object.setJavaObject(jo);
	//			//addGenericMethods(object);
	//
	//			//Helper.debug("xcv", true);
	//		}
	//		if (classname.equals("TextField")) {
	//			jo = new JTextField(arrayPlus.get(0).getTokenText());
	//			Dimension dim = new Dimension(300,30);
	//			jo.setSize(dim);
	//			jo.setPreferredSize(dim);
	//			jo.setMaximumSize(dim);
	//
	//			ret.setName(objectName);
	//			//object = ObjectC.createBuiltinObjects("TextField", false);
	//			//object.setJavaObject(jo);
	//			//addGenericMethods(object);
	//			//Helper.debug("xcv", true);
	//		}
	//		if (classname.equals("RadioButton")) {
	//			jo = new JRadioButton(arrayPlus.get(0).getTokenText());
	//			ret.setName(objectName);
	//			//object = ObjectC.createBuiltinObjects("RadioButton", false);
	//			//object.setJavaObject(jo);
	//			//	addGenericMethods(object);
	//			currentBG.add((AbstractButton) jo);
	//			//Helper.debug("xcv", true);
	//		}
	//		if (classname.equals("Label")) {
	//			jo = new JLabel(arrayPlus.get(0).getTokenText());
	//			ret.setName(objectName);
	//			jo.setForeground(Color.WHITE);
	//			//object = ObjectC.createBuiltinObjects("CheckBox", false);
	//			//object.setJavaObject(jo);
	//			//addGenericMethods(object);
	//			//Helper.debug("xcv", true);
	//		}
	//		if (classname.equals("CheckBox")) {
	//			jo = new JCheckBox(arrayPlus.get(0).getTokenText());
	//			ret.setName(objectName);
	//			//object = ObjectC.createBuiltinObjects("CheckBox", false);
	//			//object.setJavaObject(jo);
	//			//addGenericMethods(object);
	//			//Helper.debug("xcv", true);
	//		}
	//		if (jo == null) {
	//			throw new ErrorSeriousCondition("Unable to create Object " + GUI.H_ERROR + objectName + GUI.END_H_ERROR  
	//					+ " from class " + GUI.H_ERROR + classname + GUI.END_H_ERROR);
	//		}
	//		jo.setEnabled(false);
	//		javaObj.add(jo);
	//		//ret.setValue(object);
	//		RightTabPane.add2OutputDisplay(jo);
	//		RightTabPane.tab2Display(Tabs.OUTPUT);
	//		//	ret.InstanceOfBuiltinClass(bstatus);
	//		return jo;
	//	}

	//	/**
	//	 * @param className
	//	 * @return the built in class base the string argument
	//	 * @throws ErrorSeriousCondition 
	//	 */
	//	public static BIClass getBIC(String className) throws ErrorSeriousCondition {
	//		for (BIClass mcc: inClasses) {
	//			if (mcc.getName().equals(className)) {
	//				return mcc;
	//			}
	//		}
	//		throw new ErrorSeriousCondition (" Builtin Class " + className + " Not Found ");
	//	}

//	/**
//	 * Used when creating variables {@link Variable}
//	 * 
//	 * @param className
//	 * @return true if it is a Builtin class
//	 * 
//	 */
//	public static boolean isBiC(String className) {
//		for (BIClass mcc: inClasses) {
//			if (mcc.getName().equals(className)) {
//				return true;
//			}
//		}
//		for (Variable mcc: StaticObjects) {
//			if (mcc.getName().equals(className)) {
//				return true;
//			}
//		}
//		return false;
//	}

	/**
	 * @param className
	 * @return true if it is a Builtin class
	 * TODO Probably Superfluous 
	 */
	public static boolean isStaticObject(String className) {
		for (Variable mcc: StaticObjects) {
			if (mcc.getName().equals(className)) {
				return true;
			}
		}
		return false;
	}

	/**
	 * @return the inClasses
	 * @throws ErrorSeriousCondition if builtin classes have not been generated
	 */
	public static ArrayPlus<BIClass> allBuiltinClasses() {
		ArrayPlus<BIClass> bic = new ArrayPlus<BIClass>();
		for (ClassC clas : ClassC.allClasses()){
			if (clas instanceof BIClass) {
				bic.add((BIClass) clas);
			}
		}
//		if (inClasses == null || inClasses.size() < 1) {
//			throw new ErrorSeriousCondition
//			("Builtin classes have not been generated -> call registerBuiltinClasses() first");
//		}
		return bic;
		
	}

	@Override
	public abstract JComponent makeJavaComp(String theInstanceName,
			ArrayPlus<TokenType> arguments) throws ErrorCondition;
	
	public abstract String getJavaClassName();
}
